package test;

import test.bean.UserBean;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class UserInfo extends JFrame {

    private JTextField txtUsername;
    private JPasswordField passwordField;
    private JTextField txtRealname;
    private JTextField txtregion;

    private UserBean user;

    private boolean isEditable = false;

    /**
     * Launch the application.
     */
//    public static void main(String[] args) {
//        EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                try {
//                    UserInfo frame = new UserInfo();
//                    frame.setVisible(true);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    /**
     * Create the frame.
     */
    public UserInfo() {
        initialize();
    }


    public UserInfo(UserBean user) {
        this.user = user;
        initialize();
    }

    //todo
    public void initialize() {
        getContentPane().setForeground(Color.CYAN);
        setTitle("个人资料");
        setBounds(50, 25, 1200, 780);
        getContentPane().setLayout(null);

        JLabel lblUsername = new JLabel("\u6635\u79F0");
        lblUsername.setForeground(Color.WHITE);
        lblUsername.setFont(new Font("宋体", Font.BOLD, 30));
        lblUsername.setBounds(310, 47, 113, 33);
        lblUsername.setText(user.getUsername());
        getContentPane().add(lblUsername);

        JLabel lblImage = new JLabel("\u5934\u50CF");
        ImageIcon icon1 = new ImageIcon("test/image/006.jpg");
        lblImage.setIcon(icon1);
        lblImage.setBounds(57, 17, 77, 79);
        getContentPane().add(lblImage);

        txtUsername = new JTextField();
        txtUsername.setFont(new Font("宋体", Font.BOLD, 35));
        txtUsername.setBounds(367, 238, 108, 33);
        getContentPane().add(txtUsername);
        txtUsername.setColumns(10);

        int randomNumber = (int) (Math.random() * 100000);
        JLabel txtNumber = new JLabel(randomNumber + "");
        txtNumber.setFont(new Font("宋体", Font.BOLD, 25));
        txtNumber.setBounds(367, 284, 261, 39);

        getContentPane().add(txtNumber);

        passwordField = new JPasswordField();
        passwordField.setFont(new Font("宋体", Font.BOLD, 25));
        passwordField.setBounds(367, 336, 108, 32);
        getContentPane().add(passwordField);

        txtRealname = new JTextField();
        txtRealname.setFont(new Font("宋体", Font.BOLD, 25));
        txtRealname.setBounds(367, 381, 108, 38);
        getContentPane().add(txtRealname);
        txtRealname.setColumns(10);


        //按按钮后显示的图片
        ImageIcon icon61 = new ImageIcon("test/image/006.jpg");

        JRadioButton btnhead1 = new JRadioButton("\u5934\u50CF1");
        ImageIcon icon2 = new ImageIcon("test/image/001.jpg");
        btnhead1.setIcon(icon2);
        btnhead1.setPressedIcon(icon61);
        btnhead1.setBounds(383, 433, 72, 65);


        JRadioButton btnhead2 = new JRadioButton("\u5934\u50CF2");
        ImageIcon icon3 = new ImageIcon("test/image/002.jpg");
        btnhead2.setIcon(icon3);
        btnhead2.setPressedIcon(icon61);
        btnhead2.setBounds(463, 433, 72, 65);


        JRadioButton btnhead3 = new JRadioButton("\u5934\u50CF3");
        ImageIcon icon4 = new ImageIcon("test/image/003.jpg");
        btnhead3.setIcon(icon4);
        btnhead3.setPressedIcon(icon61);
        btnhead3.setBounds(556, 433, 72, 65);


        JRadioButton btnhead4 = new JRadioButton("\u5934\u50CF4");
        ImageIcon icon5 = new ImageIcon("test/image/004.jpg");
        btnhead4.setIcon(icon5);
        btnhead4.setPressedIcon(icon61);
        btnhead4.setBounds(652, 433, 72, 65);


        JRadioButton btnhead5 = new JRadioButton("\u5934\u50CF5");
        ImageIcon icon6 = new ImageIcon("test/image/005.jpg");

        btnhead5.setIcon(icon6);
        btnhead5.setPressedIcon(icon61);
        btnhead5.setBounds(758, 433, 72, 65);


        ButtonGroup btnGroup = new ButtonGroup();
        // 添加单选按钮到按钮组
        btnGroup.add(btnhead1);
        btnGroup.add(btnhead2);
        btnGroup.add(btnhead3);
        btnGroup.add(btnhead4);
        btnGroup.add(btnhead5);

        JComboBox<String> comboBox = new JComboBox<String>();
        comboBox.setFont(new Font("宋体", Font.BOLD, 25));
        comboBox.setBounds(367, 548, 108, 33);
        comboBox.addItem("中国");
        comboBox.addItem("美国");
        comboBox.addItem("日本");
        comboBox.addItem("俄罗斯");
        getContentPane().add(comboBox);

        txtregion = new JTextField();
        txtregion.setFont(new Font("宋体", Font.BOLD, 25));
        txtregion.setBounds(367, 602, 108, 33);
        getContentPane().add(txtregion);
        txtregion.setColumns(10);

        JComboBox<String> cbxyear = new JComboBox<String>();
        cbxyear.setFont(new Font("宋体", Font.BOLD, 25));
        cbxyear.setBounds(367, 502, 108, 33);
        for (int i = 0; i < 31; i++)
            cbxyear.addItem(1990 + i + "");
        getContentPane().add(cbxyear);

        JComboBox<String> cbxmonth = new JComboBox<String>();
        cbxmonth.setFont(new Font("宋体", Font.BOLD, 25));
        cbxmonth.setBounds(489, 502, 99, 33);
        for (int i = 1; i < 13; i++)
            cbxmonth.addItem(i + "");
        getContentPane().add(cbxmonth);

        JComboBox<String> cbxday = new JComboBox<String>();
        cbxday.setFont(new Font("宋体", Font.BOLD, 25));
        for (int i = 1; i < 32; i++)
            cbxday.addItem(i + "");
        cbxday.setBounds(602, 502, 99, 33);
        getContentPane().add(cbxday);

        JButton btnCancel = new JButton("主页");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //todo 返回主页面
                dispose();
                Store store = new Store(user);
                store.frame.setVisible(true);
            }
        });
        btnCancel.setBounds(1055, 458, 113, 27);
        //  btnCancel.setBorder(new RoundBorder());//设置按钮样式
        btnCancel.setContentAreaFilled(false);//设置透明
        btnCancel.setFont(new java.awt.Font("华文彩云", 1, 25));//设置字体
        btnCancel.setBorderPainted(false);//设置无边框
        getContentPane().add(btnCancel);

        JButton btnApply = new JButton("\u63D0\u4EA4");
        btnApply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                int headInt = 0;
                String username = txtUsername.getText();
                String password = new String(passwordField.getPassword());
                String realname = txtRealname.getText();
                String year = cbxyear.getSelectedItem().toString();
                String month = cbxmonth.getSelectedItem().toString();
                String day = cbxday.getSelectedItem().toString();
                String country = comboBox.getSelectedItem().toString();
                String region = txtregion.getText();
                if (btnhead1.isSelected()) headInt = 1;
                if (btnhead2.isSelected()) headInt = 2;
                if (btnhead3.isSelected()) headInt = 3;
                if (btnhead4.isSelected()) headInt = 4;
                if (btnhead5.isSelected()) headInt = 5;


                if (!"".equals(username) && !"".equals(password) && !"".equals(realname) && !"".equals(year) && !"".equals(month) && !"".equals(day) && !"".equals(country)
                        && !"".equals(region) && headInt != 0) //成功
                {
                    //全局变量设为false
                    isEditable = false;

                    //修改user对象
                    user.setUsername(username);
                    user.setPassword(password);
                    user.setCountry(country);
                    user.setHead(headInt);
                    user.setRealname(realname);
                    user.setRegion(region);

                    lblUsername.setText(user.getUsername());
                    String str = "test/image/00" + headInt + ".jpg";
                    ImageIcon icon1 = new ImageIcon(str);
                    lblImage.setIcon(icon1);

                    JOptionPane.showMessageDialog(null, "您的信息修改为：\n用户名：" + username + "\n 密码：" + password + "\n 真实姓名：" + realname + "\n 头像：" + headInt + "号头像" + "\n 出生日期："
                            + year + "年" + month + "月" + day + "日" + "\n 国家：" + country + "\n 地区：" + region + "");

                    //文本框不可编辑
                    txtUsername.setEditable(false);
                    passwordField.setEditable(false);
                    txtRealname.setEditable(false);
                    txtregion.setEditable(false);
                    cbxyear.setEnabled(false);
                    cbxmonth.setEnabled(false);
                    cbxday.setEnabled(false);
                    comboBox.setEnabled(false);
                    btnApply.setEnabled(false);

                    //隐藏头像
                    btnhead1.setOpaque(false);
                    btnhead1.setContentAreaFilled(false); //隐藏按钮的方法
                    btnhead2.setOpaque(false);
                    btnhead2.setContentAreaFilled(false); //隐藏按钮的方法
                    btnhead3.setOpaque(false);
                    btnhead3.setContentAreaFilled(false); //隐藏按钮的方法
                    btnhead4.setOpaque(false);
                    btnhead4.setContentAreaFilled(false); //隐藏按钮的方法
                    btnhead5.setOpaque(false);
                    btnhead5.setContentAreaFilled(false); //隐藏按钮的方法

                } else {
                    int abc = 0;
                    if ("".equals(username) && abc == 0) {
                        JOptionPane.showMessageDialog(null, "请填写昵称！");
                        txtUsername.requestFocus();
                        abc = 1;
                    }
                    if ("".equals(password) && abc == 0) {
                        JOptionPane.showMessageDialog(null, "请填写密码！");
                        passwordField.requestFocus();
                        abc = 1;
                    }
                    if ("".equals(realname) && abc == 0) {
                        JOptionPane.showMessageDialog(null, "请填写真实姓名！");
                        txtRealname.requestFocus();
                        abc = 1;
                    }
                    if ("".equals(region) && abc == 0) {
                        JOptionPane.showMessageDialog(null, "请填写地区！");
                        txtregion.requestFocus();
                        abc = 1;
                    }
                    if (headInt == 0 && abc == 0) {
                        JOptionPane.showMessageDialog(null, "请选择一个头像！");
                        abc = 1;
                    }

                }
            }
        });
        btnApply.setBounds(1055, 311, 113, 27);
        //btnApply.setBorder(new RoundBorder());//设置按钮样式
        btnApply.setContentAreaFilled(false);//设置透明
        btnApply.setFont(new java.awt.Font("华文彩云", 1, 25));//设置字体
        btnApply.setBorderPainted(false);//设置无边框
        getContentPane().add(btnApply);


        /**
         * 进行个人信息修改 按钮
         */
        JButton btnChange = new JButton("\u4F60\u53EF\u4EE5\u70B9\u51FB\u8FD9\u91CC\u8FDB\u884C\u4E2A\u4EBA\u4FE1\u606F\u4FEE\u6539\uFF01");
        btnChange.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //全局变量设为true
                isEditable = true;

                //启用文本框
                txtUsername.setEditable(true);
                passwordField.setEditable(true);
                txtRealname.setEditable(true);
                txtregion.setEditable(true);
                cbxyear.setEnabled(true);
                cbxmonth.setEnabled(true);
                cbxday.setEnabled(true);
                comboBox.setEnabled(true);
                btnApply.setEnabled(true);
                //点击修改的时候才显示头像

                getContentPane().add(btnhead1);
                getContentPane().add(btnhead2);
                getContentPane().add(btnhead3);
                getContentPane().add(btnhead4);
                getContentPane().add(btnhead5);
                //隐藏头像
                btnhead1.setOpaque(true);
                btnhead1.setContentAreaFilled(true); //隐藏按钮的方法
                btnhead2.setOpaque(true);
                btnhead2.setContentAreaFilled(true); //隐藏按钮的方法
                btnhead3.setOpaque(true);
                btnhead3.setContentAreaFilled(true); //隐藏按钮的方法
                btnhead4.setOpaque(true);
                btnhead4.setContentAreaFilled(true); //隐藏按钮的方法
                btnhead5.setOpaque(true);
                btnhead5.setContentAreaFilled(true); //隐藏按钮的方法

            }
        });
        //  btnChange.setBorder(new RoundBorder());//设置按钮样式
        btnChange.setFont(new java.awt.Font("宋体", 1, 25));//设置字体
        btnChange.setContentAreaFilled(false);//设置透明
        btnChange.setBorderPainted(false);//设置无边框
        btnChange.setBounds(41, 107, 724, 27);
        getContentPane().add(btnChange);

        //文本框不可编辑
        txtUsername.setEditable(false);
        passwordField.setEditable(false);
        txtRealname.setEditable(false);
        txtregion.setEditable(false);
        cbxyear.setEnabled(false);
        cbxmonth.setEnabled(false);
        cbxday.setEnabled(false);
        comboBox.setEnabled(false);
        btnApply.setEnabled(false);

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 25));
        lblNewLabel.setForeground(Color.RED);
        lblNewLabel.setBounds(538, 595, 206, 40);
        getContentPane().add(lblNewLabel);


        JLabel label = new JLabel("");
        label.setForeground(Color.RED);
        label.setFont(new Font("宋体", Font.BOLD, 25));
        label.setBounds(513, 238, 206, 40);
        getContentPane().add(label);

        JLabel label_1 = new JLabel("");
        label_1.setForeground(Color.RED);
        label_1.setFont(new Font("宋体", Font.BOLD, 25));
        label_1.setBounds(513, 336, 206, 40);
        getContentPane().add(label_1);

        JLabel label_2 = new JLabel("");
        label_2.setForeground(Color.RED);
        label_2.setFont(new Font("宋体", Font.BOLD, 25));
        label_2.setBounds(513, 381, 206, 40);
        getContentPane().add(label_2);


        txtUsername.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (isEditable && txtUsername.getText().equals("")) {
                    label.setText("请填写昵称！");

                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                if (isEditable)
                    label.setText("");
            }
        });

        passwordField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (isEditable && passwordField.getText().equals("")) {
                    label_1.setText("请填写密码！");
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                if (isEditable)
                    label_1.setText("");
            }
        });
        txtRealname.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (isEditable && txtRealname.getText().equals("")) {
                    label_2.setText("请填写真实姓名！");
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                if (isEditable)
                    label_2.setText("");
            }
        });
        txtregion.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (isEditable && txtregion.getText().equals("")) {
                    lblNewLabel.setText("请填写地区！");
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                if (isEditable)
                    lblNewLabel.setText("");
            }
        });
        ImageIcon icon = new ImageIcon("test/image/03.jpg");
        JLabel lblBackground = new JLabel(); // 创建一个标签组件对象
        lblBackground.setIcon(icon); // 设置标签组件要显示的图标
        lblBackground.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); // 设置组件的显示位置及大小
        this.getContentPane().add(lblBackground);


        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//改
    }
}

